
** build lau quarterly & annual file
if 1 {

	* Create series characteristics file
	if 1 {	
		import delimited using "$Rep_smokelabor/1_build/lau/raw/la.series.txt", clear
		saveold "$Rep_smokelabor/1_build/lau/proc/lau_series_charac.dta", replace
	}
	*** End of series module
	
	* Create measures 
	if 1 {
		
		* Loop over time segment
		foreach seg in 05-09 10-14 15-19 {
		
			import delimited using "$Rep_smokelabor/1_build/lau/raw/la.data.0.CurrentU`seg'.txt", clear
			cap drop footnote_codes
			
			* Merge in series characteristics
			merge m:1 series_id using "$Rep_smokelabor/1_build/lau/proc/lau_series_charac.dta", assert(using match) keep(match) nogen
			
			* Keep county records
			* 	For coding, see `raw'/lau/la.area_type.txt
			keep if area_type_code=="F"
			
			* Check that nothing is seasonally adjusted
			assert seasonal=="U"
			
			* Generate FIPs county code
			gen countyfip = substr(area_code,3,5)
			destring countyfip, replace
			
			* 1 row = measure*county*year*period
			gisid measure_code countyfip year period
			
			keep measure_code countyfip year period value
			
			* Loop over measures
			foreach code in 3 4 5 6 {
				preserve
					
					local measure
					if `code'==3 {
						local measure unempr
					}
					else if `code'==4 {
						local measure unemp
					}
					else if `code'==5 {
						local measure emp
					}
					else if `code'==6 {
						local measure lfp
					}
					
					keep if measure_code == `code'
					drop measure_code
					
					rename value lau_`measure'
					
					compress
					saveold "$Rep_smokelabor/1_build/lau/proc/tmpfile_lau_`measure'.dta", replace 
					
				restore
			}
			*** End of measure code loop
			
			* Merge measurements together
			use "$Rep_smokelabor/1_build/lau/proc/tmpfile_lau_unempr.dta", clear
			merge 1:1 countyfip year period using "$Rep_smokelabor/1_build/lau/proc/tmpfile_lau_unemp.dta", assert(match) nogen
			merge 1:1 countyfip year period using "$Rep_smokelabor/1_build/lau/proc/tmpfile_lau_emp.dta", assert(match) nogen
			merge 1:1 countyfip year period using "$Rep_smokelabor/1_build/lau/proc/tmpfile_lau_lfp.dta", assert(match) nogen
			
			gen rfrnc_moy = substr(period,2,2)
			destring rfrnc_moy, replace
			drop period
			
			rename year rfrnc_yr
			
			order countyfip rfrnc_yr rfrnc_moy lau_*
			
			lab var countyfip 	"FIPs county"
			lab var rfrnc_yr 	"Reference year"
			lab var rfrnc_moy 	"Reference month-of-year"
			lab var lau_unempr 	"Unemployment rate (%)"
			lab var lau_unemp 	"Unemployment (persons)"
			lab var lau_emp 	"Employment (persons)"
			lab var lau_lfp 	"Labor force participation (persons)"
			
			
			* quarterly data 
			preserve 
				drop if rfrnc_moy==13
				destring lau_*, replace force
				
				gen 	rfrnc_qtroy=1 if inrange(rfrnc_moy,1,3)
				replace rfrnc_qtroy=2 if inrange(rfrnc_moy,4,6)
				replace rfrnc_qtroy=3 if inrange(rfrnc_moy,7,9)
				replace rfrnc_qtroy=4 if inrange(rfrnc_moy,10,12)
				assert !mi(rfrnc_qtroy)
				
				* compute quarterly measure as the average across months in the quarter 
				gcollapse (mean) lau_* , by(countyfip rfrnc_yr rfrnc_qtroy)
				
				lab var lau_unempr 	"Unemployment rate (%)"
				lab var lau_unemp 	"Unemployment (persons)"
				lab var lau_emp 	"Employment (persons)"
				lab var lau_lfp 	"Labor force participation (persons)"
				
				compress
				saveold "$Rep_smokelabor/1_build/lau/proc/lau_cnty_quarterly_`seg'.dta", replace
			restore 
			
			* Annual data
			keep if rfrnc_moy==13
			drop rfrnc_moy
			destring lau_*, replace force
			
			compress
			saveold "$Rep_smokelabor/1_build/lau/proc/lau_cnty_annual_`seg'.dta", replace 
			
			* Clean up
			erase "$Rep_smokelabor/1_build/lau/proc/tmpfile_lau_unempr.dta"
			erase "$Rep_smokelabor/1_build/lau/proc/tmpfile_lau_unemp.dta"
			erase "$Rep_smokelabor/1_build/lau/proc/tmpfile_lau_emp.dta"
			erase "$Rep_smokelabor/1_build/lau/proc/tmpfile_lau_lfp.dta"
		}
		*** End of time segment loop
		
		* Append time segments
		clear
		foreach seg in 05-09 10-14 15-19 {
			append using "$Rep_smokelabor/1_build/lau/proc/lau_cnty_quarterly_`seg'.dta"
			erase "$Rep_smokelabor/1_build/lau/proc/lau_cnty_quarterly_`seg'.dta"
		}
		
		bys countyfip rfrnc_yr rfrnc_qtroy: assert _N==1
		compress
		saveold "$Rep_smokelabor/1_build/lau/proc/lau_county_quarterly.dta", replace
		
		*
		*
		clear
		foreach seg in 05-09 10-14 15-19 {
			append using "$Rep_smokelabor/1_build/lau/proc/lau_cnty_annual_`seg'.dta"
			erase "$Rep_smokelabor/1_build/lau/proc/lau_cnty_annual_`seg'.dta"
		}
		bys countyfip rfrnc_yr: assert _N==1
		compress
		saveold "$Rep_smokelabor/1_build/lau/proc/lau_county_year.dta", replace
		
	}
	*** End of module: measure creation
	
	
}

